package com.hx.miniblog.interceptor;

import com.hx.miniblog.annotation.CheckLogin;
import com.hx.miniblog.annotation.PassToken;
import com.hx.miniblog.exception.MiniBlogException;
import com.hx.miniblog.pojo.mj.RoleType;
import com.hx.miniblog.pojo.vo.LoginAccountVo;
import com.hx.miniblog.util.constant.Constant;
import com.hx.miniblog.util.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.AsyncHandlerInterceptor;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author wenhao hu
 * @title: JwtCheckInterceptor
 * @projectName miniBlog
 * @description: TODO
 * @date 2021/12/2817:51
 */
@Component
public class JwtCheckInterceptor implements AsyncHandlerInterceptor {
//
//    @Value("${config.jwt.name}")
//    String jwtHeader;
//    @Resource
//    JwtUtil jwtUtil;
//
//    @Override
//    public boolean preHandle(@NotNull HttpServletRequest request,@NotNull HttpServletResponse response,@NotNull Object handler) throws Exception {
//        if (handler instanceof HandlerMethod){
//            HandlerMethod handlerMethod = (HandlerMethod) handler;
//            String logintoken = request.getHeader(jwtHeader);
//            if (Objects.nonNull(checkAnnotation(handlerMethod, PassToken.class))){
//                return true;
//            }else if (Objects.nonNull(checkAnnotation(handlerMethod, CheckLogin.class))){
//                CheckLogin checkLogin = checkAnnotation(handlerMethod, CheckLogin.class);
//                if (jwtUtil.verifyToken(logintoken)){
//                    LoginAccountVo loginVo = JwtUtil.getLoginVo(logintoken);
//                    if (loginVo.getRole().equals(RoleType.ADMIN)){
//                        return true;
//                    }
//                    boolean check = Arrays.stream(checkLogin.roles()).collect(Collectors.toList()).contains(loginVo.getRole());
//                    if (!check){
//                        failResAuth();
//                    }
//                    return check;
//                }
//            }
//        }
//        failRes();
//        return false;
//    }
//
//    public static <A> A checkAnnotation(HandlerMethod handlerMethod,Class<? extends Annotation> annotationClass){
//
//        A annotation = null;
//        Method method = handlerMethod.getMethod();
//        if (method.isAnnotationPresent(annotationClass)) {
//            annotation = (A) method.getAnnotation(annotationClass);
//        } else {
//            Class<?> beanType = handlerMethod.getBean().getClass();
//            if (beanType.isAnnotationPresent(annotationClass)) {
//                annotation = (A) beanType.getAnnotation(annotationClass);
//            }
//        }
//        return annotation;
//    }
//    public void failRes(){
//        throw new MiniBlogException(Constant.MESSAGE.TOKEN_EXPIRE);
//    }
//    public void failResAuth(){
//        throw new MiniBlogException(Constant.MESSAGE.AUTH_PERMISSION);
//    }
}
